package com.ophiux.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
 
/**
 * @ClassName CorsConfig
 * @Description TODO
 * @Author Charon.Wang
 * @Date 2023/1/12 14:14
 * @Version 1.0
 **/
 
/**
 * 前后端跨域
 * @author Charon.Wang
 */
@Configuration
public class CorsConfig {
 
    /**
    * 当前跨域请求最大有效时长。这里默认1天
    */
    private static final long MAX_AGE = 24 * 60 * 60;
 
    @Bean
    public CorsFilter corsFilter() {
        //1. 添加 CORS配置信息
        CorsConfiguration config = new CorsConfiguration();
        //放行哪些原始域
        //config.addAllowedOrigin("*");
        //原本是addAllowedOrigin,改为addAllowedOriginPattern
        config.addAllowedOriginPattern("*"); 
        //是否发送 Cookie
        config.setAllowCredentials(true);
        //放行哪些请求方式
        config.addAllowedMethod("*");
        //放行哪些原始请求头部信息
        config.addAllowedHeader("*");
        //暴露哪些头部信息
        config.addExposedHeader("*");
        //2. 添加映射路径
        UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
        corsConfigurationSource.registerCorsConfiguration("/**",config);
        //3. 返回新的CorsFilter
        return new CorsFilter(corsConfigurationSource);
    }
}